package com.stars.home.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.stars.home.R
import com.stars.home.adapter.HomeListAdapter
import com.stars.home.base.MBRFragment
import com.stars.home.config.ConfigRx
import com.stars.home.databinding.FragmentHomeListBinding
import com.stars.home.vmodel.HomeListVModel
import com.trailer.base.net.IResp

/**
 *on 2022/9/22
 */
class OrderListFragment : MBRFragment<HomeListVModel, HomeListAdapter, FragmentHomeListBinding>(){

    override fun __before(savedInstanceState: Bundle?) {
        super.__before(savedInstanceState)
         vm.status=arguments?.getString("state")
    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        binding.refreshLayout.isEnabled = false

        vm.index.observe(this){

            try {
                adapter().removeAt(it)
            }catch (e:Exception){

            }

        }
    }

    override fun listeners() {
        super.listeners()

        binding.recyclerView!!.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val topRowVerticalPosition =
                    if (recyclerView == null || recyclerView.childCount == 0) 0 else recyclerView.getChildAt(
                        0
                    ).top
                binding.refreshTo!!.isEnabled =
                    topRowVerticalPosition >= 0 && recyclerView != null && !recyclerView.canScrollVertically(
                        -1
                    )
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }
        })


        //设置刷新监听器
        binding.refreshTo.setColorSchemeResources(
            R.color.refresh_colorPrimary,
            R.color.refresh_colorAccent,
            R.color.refresh_colorPrimaryDark
        )
        binding.refreshTo.setOnRefreshListener {
            onRefresh()
        }

    }

    override fun onListReceive(res: IResp<*>) {
        super.onListReceive(res)
        binding.refreshTo.isRefreshing = false
    }

    override fun vmClass(): Class<HomeListVModel> {
        return HomeListVModel::class.java
    }
    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentHomeListBinding {
       return FragmentHomeListBinding.inflate(layoutInflater)
    }

    override fun initAdapter(): HomeListAdapter {
        return HomeListAdapter(vm)
    }

    override fun title(): String? {
        return null
    }
    override val isRxbus: Boolean
        get() = true

    @Subscribe(tags = [Tag(ConfigRx.REFUSE_ORDER)])
    fun setRefuseOrder(position: String) {
        onRefresh()
    }
    @Subscribe(tags = [Tag(ConfigRx.TAKE_ORDER)])
    fun setTakeOrder(position: String) {
        onRefresh()
    }
    
}
